<template>
    <div>
        <el-button type="primary" @click="visible = true">增加</el-button>
        <el-dialog title="增加学生" :visible.sync="visible">
            <el-form :model="student">
                <el-form-item label="上传头像" :label-width="formLabelWidth">
                    <el-upload
                        class="avatar-uploader"
                        :action="action"
                        :show-file-list="false"
                        :on-success="handleAvatarSuccess"
                    >
                        <img v-if="imageUrl" :src="imageUrl" class="avatar" />
                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    </el-upload>
                </el-form-item>
                <el-form-item label="姓名" :label-width="formLabelWidth">
                    <el-input
                        v-model="student.name"
                        autocomplete="off"
                    ></el-input>
                </el-form-item>
                <el-form-item label="年龄" :label-width="formLabelWidth">
                    <el-input
                        v-model="student.age"
                        autocomplete="off"
                    ></el-input>
                </el-form-item>
                <el-form-item label="性别" :label-width="formLabelWidth">
                    <el-radio-group v-model="student.gender">
                        <el-radio label="男">男</el-radio>
                        <el-radio label="女">女</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="visible = false">取 消</el-button>
                <el-button type="primary" @click="handleAdd">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { add } from "../../../services/student";
import { url } from "../../../services";
export default {
    data() {
        return {
            action:url + '/upload',
            imageUrl:'',
            visible: false,
            student: {
                headImg:''
            },
            formLabelWidth: "100px",
        };
    },
    methods: {
        async handleAdd() {
            console.log(this.student);
            await add(this.student);
            this.$emit("show");
            this.visible = false;
        },
        handleAvatarSuccess(res, file) {
            // this.imageUrl = URL.createObjectURL(file.raw);
            this.student.headImg = res.filename;
            this.imageUrl = url + '/images/' + res.filename;
        }
       
    },
};
</script>

<style>
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}
.avatar-uploader .el-upload:hover {
    border-color: #409eff;
}
.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
}
.avatar {
    width: 178px;
    height: 178px;
    display: block;
}
</style>